package com.yami.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.Vip;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.admin.model.YamiSysUser;
import com.yami.shop.security.admin.util.SecurityUtils;
import com.yami.shop.service.VipService;
import io.swagger.v3.oas.annotations.Operation;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/vip")
@Tag(name = "会员信息",description = "会员信息")
public class VipController {

    @Autowired
    private VipService vipService;



    /**
     * 保存会员信息
     * @param vip 会员信息
     * @return 保存结果
     */
    @PostMapping("/save")
    @Operation(description = "保存会员信息")
    public ServerResponseEntity<String> save(@RequestBody  Vip vip) {
        YamiSysUser sysUser = SecurityUtils.getSysUser();
        vip.setCreateBy(sysUser.getUserId().toString());
        vipService.saveVip(vip);
        return ServerResponseEntity.success();
    }


    @PostMapping("/update")
    @Operation(description = "更新会员信息")
    public ServerResponseEntity<String> update(@RequestBody  Vip vip) {
        vipService.updateById(vip);
        return ServerResponseEntity.success();
    }


    @GetMapping("/page")
    @Operation(description = "分页获取会员信息")
    public ServerResponseEntity<IPage<Vip>> page(
            Vip vip, PageParam<Vip> page
    ){
        IPage<Vip> vipIPage = vipService.page(page, new LambdaQueryWrapper<Vip>()
                .like(StrUtil.isNotBlank(vip.getName()), Vip::getName, vip.getName())
                .eq(vip.getStatus() != null, Vip::getStatus, vip.getStatus()));
        return ServerResponseEntity.success(vipIPage);
    }
}
